package com.fhd.service.House;

import com.fhd.dao.BaseDao;
import com.fhd.dao.House.HouseDao;
import com.fhd.dao.House.HouseDaoImplement;
import com.fhd.pojo.House;
import com.fhd.pojo.Merchant;

import java.sql.Connection;
import java.util.ArrayList;

public class HouseServiceImplement implements HouseService {
    // 调用Dao层
    private HouseDao houseDao = null;
    public HouseServiceImplement() {
        houseDao = new HouseDaoImplement();
    }

    // 获取某个商户的所有房型
    @Override
    public ArrayList<House> getSomeoneHouse(int merchantId) {
        Connection conn = null;
        ArrayList<House> houses = null;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            houses = houseDao.getSomeoneHouse(conn,merchantId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return houses;
    }

    // 增加房型
    @Override
    public int addHouse(int merchantId, String houseName, int price, int remainNumber, String keyWord) {
        Connection conn = null;
        int affectedRow = 0;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            affectedRow = houseDao.addHouse(conn,merchantId,houseName,price,remainNumber,keyWord);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return affectedRow;
    }

    // 删去房型
    @Override
    public int deleteHouse(int id) {
        Connection conn = null;
        int affectedRow = 0;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            affectedRow = houseDao.deleteHouse(conn,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return affectedRow;
    }

    // 修改房型信息
    @Override
    public int updateHouse(int id, String houseName, int price, int remainNumber, String keyWord) {
        Connection conn = null;
        int affectedRow = 0;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            affectedRow = houseDao.updateHouse(conn,id,houseName,price,remainNumber,keyWord);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return affectedRow;
    }
}
